import {Tabs, Divider, message} from 'antd'
import React, {useState, useRef, useEffect} from 'react'
import {PageHeaderWrapper} from '@ant-design/pro-layout'
import ProTable, {ProColumns, ActionType} from '@ant-design/pro-table'
import { connect, Link } from 'umi'
import {getSalaryChangeConfirmPage, saveAndUpdateSalaryChange} from "@/pages/Salary/service";
import {BPMStatusEnum, salaryAdjustmentTypeList} from "@/utils/constant";
import moment from 'moment'

const {TabPane} = Tabs;

const TableList = ({global}) => {
  const {options = {}} = global;
  const {jobLevel = []} = options;
  const actionRef1 = useRef();
  const actionRef2 = useRef();
  const [tabKey, setTabKey] = useState('0');

  const tabChange = (key) => {
    setTabKey(key);
    if (key === '0') {
      actionRef1.current && actionRef1.current.reload();
    } else {
      actionRef2.current && actionRef2.current.reload();
    }
  };

  const itemEdit = (item) => {
    saveAndUpdateSalaryChange({...item, status: 1}).then(res => {
      if (res.flag) {
        message.success('确认薪资变动成功!')
      } else {
        message.error(res.error)
      }
      actionRef1.current && actionRef1.current.reload();
    })
  };

  const columns = [
    {
      title: '工号',
      dataIndex: 'code',
      hideInSearch: true,
    },
    {
      title: '姓名',
      dataIndex: 'name',
      width:60,
      render: (item, record) => (
        <Link to={`/info/details/${record.oid}`}>{item}</Link>
      ),
    },
    {
      title: '生效日期',
      dataIndex: 'effectiveTime',
      hideInSearch: true,
      render: (text) => {
        return text !== '-' ? moment(text).format('YYYY-MM-DD') : ''
      }
    },
    {
      title: '变动类型',
      dataIndex: 'type',
      hideInSearch: true,
      valueEnum: {
        0: {text: salaryAdjustmentTypeList[0].label},
        1: {text: salaryAdjustmentTypeList[1].label},
        2: {text: salaryAdjustmentTypeList[2].label},
        3: {text: salaryAdjustmentTypeList[3].label},
        4: {text: salaryAdjustmentTypeList[4].label},
        5: {text: salaryAdjustmentTypeList[5].label},
        6: {text: salaryAdjustmentTypeList[6].label},
        7: {text: salaryAdjustmentTypeList[7].label},
      },
    },
    {
      title: '公司',
      dataIndex: 'newOrgName',
      hideInSearch: true,
      render: (text,record) =>(text !== '-' ? text : record.orgname)
    },
    {
      title: '所属部门',
      dataIndex: 'newUnitName',
      hideInSearch: true,
      render: (text,record) =>(text !== '-' ? text : record.unitname)
    },
    {
      title: '任职岗位',
      dataIndex: 'newJobName',
      hideInSearch: true,
      render: (text,record) =>(text !== '-' ? text : record.jobname)
    },
    {
      title: '职级',
      dataIndex: 'newJobLevelId',
      hideInSearch: true,
      render: (text,record) => {
        return text !== '-' ?
          jobLevel.find(item => text ===item.id)?.name :
          jobLevel.find(item => record.jobLevelId ===item.id)?.name
      }
    },
    {
      title: '现月薪标准',
      dataIndex: 'standardMonthlySalary',
      hideInSearch: true,
    },
    {
      title: '现基本工资基数',
      dataIndex: 'baseSalary',
      hideInSearch: true,
    },
    {
      title: '现模块工资基数',
      dataIndex: 'moduleSalaryBase',
      hideInSearch: true,
    },
    {
      title: '申请调整月薪标准',
      dataIndex: 'applyAdjustmentMonthlySalary',
      hideInSearch: true,
    },
    {
      title: '申请调整基本工资基数',
      dataIndex: 'applyBaseSalary',
      hideInSearch: true,
    },
    {
      title: '申请调整模块工资基数',
      dataIndex: 'applyModuleSalaryBase',
      hideInSearch: true,
    },
    {
      title: '拟调整津贴补助',
      dataIndex: 'proposedAdjustmentAllowance',
      hideInSearch: true,
    },
    /*{
      title: '审批状态',
      dataIndex: 'status',
      hideInSearch: true,
      valueEnum: BPMStatusEnum
    },*/
    {
      title: '操作',
      dataIndex: 'option',
      hideInSearch: true,
      render: (item, record) => (<>
        {record.bpmUrl && <a onClick={() => window.open(record.bpmUrl)}>审批详情</a>}
        {(tabKey === '0' && record.bpmUrl) && <Divider type="vertical"/>}
        {tabKey === '0' && <a onClick={() => itemEdit(record)}>确认</a>}
      </>),
    },
  ];

  return (
    <PageHeaderWrapper>
      <Tabs animated={false} defaultActiveKey="0" onChange={tabChange}>
        <TabPane tab="待办理" key="0">
          <ProTable
            actionRef={actionRef1}
            rowKey="id"
            request={(params) => getSalaryChangeConfirmPage(params, 2, 0)}
            columns={columns}
          />
        </TabPane>
        <TabPane tab="已办理" key="1">
          <ProTable
            actionRef={actionRef2}
            rowKey="id"
            request={(params) => getSalaryChangeConfirmPage(params, 2, 1)}
            columns={columns}
          />
        </TabPane>
      </Tabs>

    </PageHeaderWrapper>
  )
};

export default connect(({global}) => ({
  global
}))(TableList)
